package com.chapter3.search;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

import com.util.StdIn;
import com.util.StdOut;
import com.util.TimeCounter;

/** 
 * @ClassName: FrequencyCounter 
 * @Description: TODO(这里用一句话描述这个类的作用) 
 * @author minjun minjun@bw30.com
 * @date 2015-2-18 上午5:25:18 
 *  
 */
public class FrequencyCounter {

    /**
     * Reads in a command-line integer and sequence of words from
     * standard input and prints out a word (whose length exceeds
     * the threshold) that occurs most frequently to standard output.
     * It also prints out the number of words whose length exceeds
     * the threshold and the number of distinct such words.
     * @throws FileNotFoundException 
     */
    public static void main(String[] args) throws FileNotFoundException {
    	TimeCounter.start();
//        int distinct = 0, words = 0;
        int minlen = 10;
        ST<String, Integer> st =
        		//低效的基于无序链表实现的符号表
//        		new SequentialSearchST<String, Integer>();
        		new BinarySearchST<String, Integer>();
        StdIn.setScanner(new Scanner(
					//new java.io.BufferedInputStream(System.in)
					new FileInputStream("F:\\算法\\attach\\leipzig1M.txt")
					, "utf-8"));
        //tinyTale
        //tale
        //leipzig1M
        // compute frequency counts
        while (!StdIn.isEmpty()) {
            String key = StdIn.readString();
            if (key.length() < minlen) continue;
//            words++;
            if (st.contains(key)) {
                st.put(key, st.get(key) + 1);
            }
            else {
                st.put(key, 1);
//                distinct++;
            }
        }

        // find a key with the highest frequency count
        String max = "";
        st.put(max, 0);
        for (String word : st.keys()) {
            if (st.get(word) > st.get(max))
                max = word;
        }

        StdOut.println(max + " " + st.get(max)+",总耗时"+TimeCounter.count()/1000.0+"秒");
//        StdOut.println("distinct = " + distinct);
//        StdOut.println("words    = " + words);
    }
}
